#include <QVBoxLayout>
#include <QListWidget>

#include "videolinkwindow.h"
#include "../database/databasemanager.h"

namespace Jcr
{
class VideoLinkWindowPrivate
{
public:
    VideoLinkWindowPrivate(VideoLinkWindow* q);
    VideoLinkWindow* q_ptr;
    QStringList videoLinks;
    QListWidget* listWidget;
};

VideoLinkWindowPrivate::VideoLinkWindowPrivate(VideoLinkWindow *q) : q_ptr(q)
{
    listWidget = new QListWidget(q_ptr);
    listWidget->setAlternatingRowColors(true);

    QVBoxLayout* layout = new QVBoxLayout;
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(listWidget);
    q_ptr->setLayout(layout);
}

VideoLinkWindow::VideoLinkWindow(QWidget *parent) : ModelWindow(parent)
{
    d_ptr = new VideoLinkWindowPrivate(this);
}

VideoLinkWindow::~VideoLinkWindow()
{
    delete d_ptr;
}

void VideoLinkWindow::exec()
{
    Jcr::DatabaseManager* dbMgr = Jcr::DatabaseManager::instance();
    foreach (auto link, d_ptr->videoLinks)
        dbMgr->addModelVideoLink(this->modelName(), link);
}

void VideoLinkWindow::clear()
{
    d_ptr->listWidget->clear();
}

void VideoLinkWindow::setLinks(const QStringList& links)
{
    if (d_ptr->videoLinks != links) {
        d_ptr->videoLinks = links;

        d_ptr->listWidget->clear();
        foreach (auto link, links) {
            QListWidgetItem* item = new QListWidgetItem;
            item->setText(link);
            d_ptr->listWidget->addItem(item);
        }
    }
}

QStringList VideoLinkWindow::links() const
{
    return d_ptr->videoLinks;
}


} // namespace Jcr
